package com.ztgf.commodity.api.cms;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.commodity.models.bo.CommodityAdminListBO;
import com.ztgf.commodity.models.bo.CommodityAdminShopListBO;
import com.ztgf.commodity.models.bo.CommodityAuditBO;
import com.ztgf.commodity.models.bo.CommodityDecorationBO;
import com.ztgf.commodity.models.service.ICommodityCategoryService;
import com.ztgf.commodity.models.service.ICommodityInfoService;
import com.ztgf.commodity.models.vo.*;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * author: chenyu.wang
 * date: 2020-05-25 14:59
 * Description:
 */
@RestController
@RequestMapping("/cms/commodityInfo")
@Slf4j
@Api(value = "CommodityInfoController", tags = "商品表cms接口")
public class CommodityInfoController {

    @DubboReference
    private ICommodityInfoService commodityInfoService;

    @DubboReference
    private ICommodityCategoryService commodityCategoryService;

    @PostMapping(value = "/queryCenterGoodsDetails")
    @ApiOperation(value = "查看产品详情", notes = "查看产品详情")
    public Result queryCenterGoodsDetails(Long id) {
        if (null != id) {
            CommodityRegardVO vo = commodityInfoService.queryAdminCenterGoodsDetails(id);
            return new Result<>(vo);
        } else {
            return new Result<>(CodeEnum.FAIL_100002);
        }
    }

    @PostMapping(value = "/updateAuditCommodity")
    @ApiOperation(value = "后台管理系统审核商品", notes = "后台管理系统审核商品")
    public Result updateAuditCommodity(Long id, Long userId, String text, Integer state) {
        if (null != id) {
            return commodityInfoService.updateAuditCommodity(id, userId, text, state);
        } else {
            return new Result<>(CodeEnum.FAIL_100002);
        }
    }

    @PostMapping(value = "/queryAdminPageList")
    @ApiOperation(value = "后台管理系统商品列表", notes = "后台管理系统商品列表")
    public Result queryAdminPageList(CommodityAdminListBO bo) {
        Page<CommodityAdminListVO> page = commodityInfoService.queryAdminPageList(bo);
        return new Result<>(page);
    }

    @PostMapping(value = "/queryAuditAdminList")
    @ApiOperation(value = "后台管理系统产品审核列表", notes = "后台管理系统产品审核列表")
    public Result queryAuditAdminList(CommodityAuditBO bo) {
        Page<CommodityAuditVO> page = commodityInfoService.queryAuditAdminList(bo);
        return new Result<>(page);
    }

    @PostMapping(value = "/updateViolation")
    @ApiOperation(value = "平台操作违规删除/下架商品", notes = "平台操作违规删除/下架商品")
    public Result updateViolation(Long id, Integer type, String content) {
        if (null == id) {
            return new Result<>(CodeEnum.FAIL_100002);
        } else {
            return commodityInfoService.updateViolation(id, type, content);
        }
    }

    @PostMapping(value = "/updateBatchAuditCommodity")
    @ApiOperation(value = "后台管理系统批量审核产品", notes = "后台管理系统批量审核产品")
    public Result updateBatchAuditCommodity(String ids, Integer state) {
        if (!StringUtils.isNotBlank(ids)) {
            return new Result<>(CodeEnum.FAIL_100002);
        } else {
            return commodityInfoService.updateBatchAuditCommodity(ids, state);
        }
    }

    @PostMapping(value = "/queryAdminShopCommodityList")
    @ApiOperation(value = "后台管理系统店铺所有产品列表", notes = "后台管理系统店铺所有产品列表")
    public Result queryAdminShopCommodityList(CommodityAdminShopListBO bo) {
        Page<CommodityAdminShopListVO> page = commodityInfoService.queryAdminShopCommodityList(bo);
        return new Result<>(page);
    }

    @PostMapping(value = "/queryDecorationCommodityList")
    @ApiOperation(value = "查询所有上架中的产品，用于平台频道装修页", notes = "查询所有上架中的产品，用于平台频道装修页")
    public Result queryDecorationCommodityList(CommodityDecorationBO bo) {
        List<CommodityDecorationVO> list = commodityInfoService.queryDecorationCommodityList(bo);
        return new Result<>(list);
    }

    @GetMapping(value = "/batchExportAdminGoodsList/{ids}")
    @ApiOperation(value = "批量导出商品", notes = "批量导出商品")
    public void batchExportAdminGoodsList(HttpServletResponse response, @PathVariable String ids) {
        //创建一个数组用于设置表头
        String[] attributeNames = new String[]{"商品名称", "规格型号", "品牌名称", "店铺名称", "价格"};
        //查询订单
        String[] idList = ids.split(",");
        List<CommodityAdminExportVO> goodsList = new ArrayList<>();
        for (String id : idList) {
            List<CommodityAdminExportVO> list = commodityInfoService.getAdminExportGoods(Long.valueOf(id));
            goodsList.addAll(list);
        }
        ExcelExport2.export(response, goodsList, attributeNames, "_产品");
    }
    @PostMapping(value = "/batchExport")
    @ApiOperation(value = "批量导出商品", notes = "批量导出商品")
    public void batchExport(HttpServletResponse response, CommodityAdminListBO bo) {
        //查询订单
        List<CommodityExportNewVO> list = commodityInfoService.queryExportList(bo);
        if(list == null){
            list = new ArrayList<>();
        } else {
            //查询分类
            Map<String, String> map = new HashMap<>();
            List<CommodityCategoryTreeVO> treeList = commodityCategoryService.queryNewCategoryTree();
            doParentCategory(map, treeList);

            for(CommodityExportNewVO exportNewVO : list){
                exportNewVO.setCategoryName(map.get(exportNewVO.getCategoryId()));
            }
        }
        ExcelUtils.exportExcel(list,"商品列表","商品列表", CommodityExportNewVO.class,"商品列表"+ DateUtils.dateToStr(new Date(), DateUtils.DATE_FORMAT_DATESHORTDAY) +".xlsx",response);
    }

    private void doParentCategory(Map<String, String> map, List<CommodityCategoryTreeVO> treeList){
        if(treeList != null && treeList.size() > 0){
            for(CommodityCategoryTreeVO treeVO : treeList){
                String parentNames = map.get(String.valueOf(treeVO.getParentId()));
                if(StrUtils.isEmpty(parentNames)){
                    parentNames = treeVO.getName();
                } else {
                    parentNames = parentNames + ">" + treeVO.getName();
                }

                map.put(treeVO.getId().toString(), parentNames);

                if(treeVO.getChildList() != null && treeVO.getChildList().size() > 0){
                    doParentCategory(map, treeVO.getChildList());
                }
            }
        }
    }

    @PostMapping(value = "/queryAdminGoodsRecycleBin")
    @ApiOperation(value = "后台管理系统产品回收站", notes = "后台管理系统产品回收站")
    public Result queryAdminGoodsRecycleBin(CommodityAdminListBO bo) {
        Page<CommodityAdminListVO> page = commodityInfoService.queryAdminGoodsRecycleBin(bo);
        return new Result<>(page);
    }

    @PostMapping(value = "/doImageUrlWM")
    @ApiOperation(value = "处理商品图片加水印（旧数据）", notes = "处理商品图片加水印（旧数据）")
    public Result doImageUrlWM() {
        commodityInfoService.doImageUrlWM();
        return new Result<>();
    }

    @PostMapping(value = "/doImageWM")
    @ApiOperation(value = "图片加水印（旧图片）", notes = "图片加水印（旧图片）")
    @ApiImplicitParam(name = "path",value = "文件夹路径",required = true,paramType = "query", dataType = "String")
    public Result doImageWM(String path) {
        ImagesWmUtils.doWm(path);
        return new Result<>();
    }
}
